<?php

namespace app\admin\controller\mall;

use app\admin\traits\Curd;
use app\common\controller\AdminController;
use app\common\lib\Show;
use app\common\lib\Status;
use app\common\model\MachineMachine;
use app\common\model\MallOrderGoods;
use EasyAdmin\annotation\ControllerAnnotation;
use app\common\model\MallOrder;
use app\common\model\MallUser;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="订单管理")
 */
class Order extends AdminController
{
    use Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = (new MallOrder());
        $this->field = "*,openid as wechat,id as get_goods";

    }

    /**
     * @return array|mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where, $map) = $this->buildTableParames(['machine.machine_name', 'cate.title', 'wechat.nickName'], 'o');
            if (!empty($map)) {
                if (!empty($map['machine.machine_name'])) {
                    $where[] = [
                        ['m.machine_name', 'like', "%{$map['machine.machine_name']}%"]
                    ];
                }
                if (!empty($map['cate.title'])) {
                    $where[] = [
                        ['c.title', 'like', "%{$map['cate.title']}%"]
                    ];
                }
                if (!empty($map['wechat.nickName'])) {
                    $where[] = [
                        ['w.nickName', 'like', "%{$map['wechat.nickName']}%"]
                    ];
                }
            }
            if (!empty($this->request->cate_machine)) {
                $machine = (new MachineMachine())
                    ->where('machine_cate', $this->request->cate_machine)
                    ->select()->toArray();
                $where[] = [
                    ['o.machine_id', 'in', array_column($machine, 'machine_id')]
                ];
            }
            $lists = $this->model
                ->field('o.*')
                ->alias('o')
                ->join('machine_cate c', 'o.cate_id = c.id')
                ->join('machine_machine m', 'o.machine_id = m.machine_id')
                ->join('weixin_user w', 'o.openid = w.openid')
                ->where($where)
                ->with(['cate', 'machine', 'wechat'])
                ->order("o.id desc")
                ->paginate($limit)
                ->toArray();
            return Show::json_show(Status::lay_normal, "ok", $lists['data'], $lists['total']);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->getBaseById($id,'*','wechat');
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        switch ($row['order_status']){
            case 1:
                $row['status_name'] = "支付完成";
                break;
            case 0:
                $row['status_name'] = "未支付";
                break;
        }
        $goodslist = (new MallOrderGoods())->where('order_id', $row['id'])->select()->toArray();
        $this->assign('goodslist', $goodslist);
        $this->assign('row', $row);
        return $this->fetch();
    }
}